<?php
use \Workerman\Worker;
use \Workerman\Connection\AsyncTcpConnection;
use Workerman\Lib\Timer;

require_once __DIR__ . '/vendor/autoload.php';

// 代理监听本地4406端口
$server = new Worker('fsync://0.0.0.0:3899');

$server->name = 'fsync';

$server->onWorkerStart = function ($connection) {
    $client = new AsyncTcpConnection("fsync://127.0.0.1:3899");
    $client->connect();

    Timer::add(1, function() use ($client){
        $files = glob("start*.php");
        foreach($files as $file) {
            $client->send(array(
                'filename' => $file,
                'content' => file_get_contents($file),
            ));
        }
    }, []);
};

$server->onWorkerStop = function ($worker){

};

$server->onMessage = function($connection, $buffer) {
    echo $connection->id;
    echo "==========================\n";
    echo ($buffer['filename']);echo "\n";
};

// 如果不是在根目录启动，则运行runAll方法
if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}
